package com.zjb.home.boxingtu.view.other;

import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

import java.text.SimpleDateFormat;

/**
 * Created by zhangjiebo on 2019/10/24.
 *
 * @author ZhangJieBo
 */
public class ClockTextView extends AppCompatTextView {

    String pattern = "MM-dd HH:mm:ss";
    private Runnable runnable;

    public ClockTextView(Context context) {
        super(context);
    }

    public ClockTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ClockTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setPattern(String pattern) {
        this.pattern = pattern;
        if (runnable!=null){
            removeCallbacks(runnable);
            runnable = null;
        }
        runnable = () -> {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            String dateTime = sdf.format(System.currentTimeMillis());
            postDelayed(runnable,1000);
            setText(dateTime);
        };
        post(runnable);
    }
}
